using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UILevelGameOver : UIEntityWindow
    {
        #region Template Generate,don't modify

        protected class UIB_UILevelGameOver
        {
            #region ObjectBinding Generate

            public IQIGame.Onigao.Framework.ExButton btnRevive { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnRevive", out var __tbv0);
                this.btnRevive = __tbv0;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        #region Window Config

        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UILevelGameOver),
            layer = EUILayer.Middle,
            activeBlurBg = true,
            blurDownSampling = 4,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #endregion Window Config

        /// <summary>
        /// 因为添加了场景模糊背景 需要延迟到场景摄像机被设置以后再显示
        /// </summary>
        public static async UniTaskVoid ShowAfterCameraEnable()
        {
            await UniTask.WaitUntil(() => Camera.main != null);
            ManagerCenter.UI.ShowWindowAsync<UILevelGameOver>().Forget();
        }

        #region fields

        #endregion fields

        #region properties

        protected UIB_UILevelGameOver ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UILevelGameOver();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
        }

        protected override void OnShow(UIBaseData data)
        {
        }

        protected override void OnHide()
        {
        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            ui.btnRevive.AddClickEvent(OnClickBtnRevive);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {
        }

        protected override void OnDispose()
        {
        }

        #region methonds

        private void OnClickBtnRevive()
        {
            RequestRevive().Forget();
        }

        private async UniTaskVoid RequestRevive()
        {
            var result = await CLevelLogicModule.NetCore.CS_RequestRevive();
            if (result.code == 0)
            {
                Close();
            }
        }

        #endregion methonds
    }
}